package com.yh.util;

import com.yh.service.RedisLock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author yh
 */
@Component
public class RedisLockUtil {
    private RedissonClient redissonClient;

    public RedisLockUtil(RedissonClient redissonClient){
        this.redissonClient = redissonClient;
    }

    public RLock lock(String lockKey){
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock();
        return lock;
    }

    public void unlock(String lockKey){
        RLock lock = redissonClient.getLock(lockKey);
        lock.unlock();
    }

    public RLock lock(String lockKey,int lockTime,TimeUnit unit){
        RLock lock = redissonClient.getLock(lockKey);
        lock.lock(lockTime, unit);
        return lock;
    }
}
